import {getLyric} from 'api/song'
import {ERR_OK} from 'api/config'
import {Base64} from 'js-base64'

export default class Song {
  constructor({id, mid, singer, name, album, duration, image, url}) {
    this.id = id
    this.mid = mid
    this.singer = singer
    this.name = name
    this.album = album
    this.duration = duration
    this.image = image
    this.url = url
  }

  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }

    return new Promise((resolve, reject) => {
      getLyric(this.mid).then((res) => {
        if (res.retcode === ERR_OK) {
          this.lyric = Base64.decode(res.lyric)
          resolve(this.lyric)
        } else {
          reject('no lyric')
        }
      })
    })
  }
}

export function createSong(musicData) {
  const music = randomMusic()
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: filterImage(music,musicData.albummid),
    url: filtersMusicUrl(music,musicData.songid)
  })
}

function filterSinger(singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  return ret.join('/')
}

const filterImage = (music,albummid)=>{
    if(typeof(albummid) == 'string'){
        return `https://y.gtimg.cn/music/photo_new/T002R300x300M000${albummid}.jpg?max_age=2592000`
    }else{
        return music.picture
    }
}
const filtersMusicUrl = (music,songid) =>{
    if(typeof(songid) == 'string'){
        return `http://ws.stream.qqmusic.qq.com/${songid}.m4a?fromtag=46`
    }else{
        return music.url
    }
}

function randomMusic(){
    const list = [
        {
            "title": "江南",
            "artist": "林俊杰",
            "picture": "https://cdnmusic.migu.cn/picture/2019/0911/0225/AS2b9ad93e51c841a0bcaf5e90ef8dcb7a.jpg",
            "url": "http://tyst.migu.cn/public/product5th/product35/2019/10/1616/2011年08月10日00011批次/歌曲下载/MP3_40_16_Stero/60084600029.mp3",
            "sid": "0",
            "ssid": "ss1001"
        },
        {
            "title": "改变自己",
            "artist": "王力宏",
            "picture": "https://cdnmusic.migu.cn/picture/2019/1206/1742/AS1df0c68406d045e3b04184bcbd02597f.jpg",
            "url": "http://tyst.migu.cn/public/product5th/product35/2019/10/1616/2011年08月10日00011批次/歌曲下载/MP3_40_16_Stero/60084600029.mp3",
            "sid": "1",
            "ssid": "ss1002"
        },
        {
            "title": "爱上你等于爱上寂寞",
            "artist": "那英",
            "picture": "https://cdnmusic.migu.cn/picture/2019/1104/1700/AS3a01fdff9a7145b7b326cded8db66602.jpg",
            "url": "http://tyst.migu.cn/public/product5th/product30/2019/03/28/2018年09月27日08点15分批量项目华纳75首-3/歌曲下载/MP3_40_16_Stero/6005751G5AU.mp3",
            "sid": "2",
            "ssid": "ss1003"
        },
        {
            "title": "光芒",
            "artist": "张靓颖",
            "picture": "https://cdnmusic.migu.cn/picture/2019/0523/0842/AS8f911d4d9f3c469fa75c7ed969be9588.jpg",
            "url": "http://tyst.migu.cn/public/product08/2018/03/07/2014年08月25日华谊兄弟预留内容准入258首/歌曲下载/MP3_40_16_Stero/光芒-张靓颖.mp3",
            "sid": "3",
            "ssid": "ss1004"
        },
        {
            "title": "怒放的生命",
            "artist": "汪峰",
            "picture": "https://cdnmusic.migu.cn/picture/2019/1226/1421/AS2ab219ed1c8e43598b585b234fa784db.jpg",
            "url": "http://tyst.migu.cn/public/product7th/productB12/2020/05/2718/2019年12月24日16点50分批量项目咪咕Believe1100首-9/歌曲下载/MP3_40_16_Stero/6993162SUZY182748.mp3",
            "sid": "4",
            "ssid": "ss1005"
        },
        {
            "title": "裂缝中的阳光",
            "artist": "林俊杰",
            "picture": "https://cdnmusic.migu.cn/picture/2020/0208/1635/ASf12b4f1c7aefab39a686261918224657.jpg",
            "url": "http://tyst.migu.cn/public/product8th/product38/2020/03/1712/2020年01月25日00点00分紧急内容准入华纳5首122858/歌曲下载/MP3_40_16_Stero/6005752BP89.mp3",
            "sid": "5",
            "ssid": "ss1006"
        },
        {
            "title": "有理想(数字专辑)",
            "artist": "李荣浩",
            "picture": "https://cdnmusic.migu.cn/picture/2020/0519/1728/AS44d5da3bf1492e98b2cfbd82c825d9d9.jpg",
            "url": "http://tyst.migu.cn/public/product23/2018/11/15/2017年07月24日11点47分内容准入华纳11首/歌曲下载/MP3_40_16_Stero/6005759Z0MB.mp3",
            "sid": "6",
            "ssid": "ss1007"
        },
        {
            "title": "最初的梦想",
            "artist": "范玮琪",
            "picture": "https://cdnmusic.migu.cn/picture/2019/1104/1435/AS16f329b8a2504c2d96315baa14dc507b.jpg",
            "url": "http://tyst.migu.cn/public/product5th/product35/2019/10/1616/福茂三月申报歌曲/歌曲下载/MP3_40_16_Stero/60083600445.mp3",
            "sid": "7",
            "ssid": "ss1008"
        },
        {
            "title": "相信",
            "artist": "苏打绿",
            "picture": "https://cdnmusic.migu.cn/picture/2019/1230/1035/AL75821493f39a4f85a01d94884adaab6c.jpg",
            "url": "http://tyst.migu.cn/public/product6th/productB09/2020/01/0915/2019年12月19日14点20分批量项目咪咕Believe145首-16/歌曲下载/MP3_40_16_Stero/6993160UML8.mp3",
            "sid": "8",
            "ssid": "ss1009"
        },
        {
            "title": "就现在",
            "artist": "吴莫愁",
            "picture": "https://cdnmusic.migu.cn/picture/2020/0311/1807/AS2285d9a170aad1e1e21f0b9f637f2698.jpg",
            "url": "http://tyst.migu.cn/public/ringmaker01/n4/karakalupload/2014/05/2014年5月27日紧急准入梦响强音1首/歌曲下载/MP3_40_16_Stero/就现在-吴莫愁.mp3",
            "sid": "9",
            "ssid": "ss1010"
        },
        {
            "title": "有种(电影《流浪地球》推广曲)",
            "artist": "火箭少女101孟美岐",
            "picture": "https://cdnmusic.migu.cn/picture/2019/0115/1156/AS222c734d826047f49ac49a896355b18d.jpg",
            "url": "http://tyst.migu.cn/public/product8th/product39/2020/04/0814/2019年01月15日11点19分紧急内容准入咪咕音乐OST421首/歌曲下载/MP3_40_16_Stero/69911800342144627.mp3",
            "sid": "10",
            "ssid": "ss1011"
        }
    ]
    const index = Math.floor(Math.random()*10)
    const music = list[index]
    return music
}

